package com.yoosora.plugins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.mikuac.shiro.annotation.MessageHandler;
import com.mikuac.shiro.common.utils.MsgUtils;
import com.mikuac.shiro.common.utils.RegexUtils;
import com.mikuac.shiro.common.utils.ShiroUtils;
import com.mikuac.shiro.core.Bot;
import com.mikuac.shiro.core.BotPlugin;
import com.mikuac.shiro.dto.event.message.GroupMessageEvent;
import com.mikuac.shiro.dto.event.message.WholeMessageEvent;
import com.yoosora.common.consts.ConstsS;
import com.yoosora.model.SetuDTO;
import com.yoosora.utils.ApiConst;
import com.yoosora.utils.RegexConst;
import com.yoosora.utils.RequestUtils;
import com.yoosora.utils.Strings;
import com.yoosora.utils.YoosoraUtils;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class SetuPlugin extends BotPlugin {
	private static String R18 = "2";
	@MessageHandler(cmd = RegexConst.SETU)
	public void setuMsg(@NotNull Bot bot, @NotNull WholeMessageEvent event){
		String message = event.getMessage();
		long groupId = event.getGroupId();
		int messageId = event.getMessageId();
		bot.sendMsg(event,MsgUtils.builder().text("少女祈祷中...").reply(messageId).build(),false );
		message = message.replace("#", "#num:");
		message = message.replace("uid", "#uid:");
		message = message.replace("tag", "#tag:");
		String[] paramStr = message.split("#");
		Map<String, String> param = getParam(paramStr);
		String result = RequestUtils.builder().get().build(ApiConst.SETU_API, null, null, param);
		//		System.out.println(result);
		JSONObject jsonObject = JSON.parseObject(result);
		JSONArray array = jsonObject.getJSONArray("data");
		if(array.size()==0){
			bot.sendMsg(event, "未查询到涩图", false);
		}
		List<SetuDTO> setuDTOS = array.toJavaList(SetuDTO.class);
		List<String> msgList = setuDTOS.stream().map(o ->  
				MsgUtils.builder()
					.text("作品名：").text(o.getTitle()).text(ConstsS.RN)
					.text("画师：").text(o.getAuthor()).text(ConstsS.RN)
					.text("画师uid：").text(o.getUid()).text(ConstsS.RN)
					.text("作品pid：").text(o.getPid()).text(ConstsS.RN)
					.text("作品链接：")
					.text(String.format("https://www.pixiv.net/artworks/%s", o.getPid()))
					.text(ConstsS.N)
					.img(o.getUrls().getRegular())
//					.img(o.getUrls().getOriginal())
					.build()
		).collect(Collectors.toList());
		List<Map<String, Object>> list = ShiroUtils.generateForwardMsg(2315160273L, "博麗の巫女", msgList);
		bot.sendGroupForwardMsg(groupId,list);
	}
	
	@MessageHandler(cmd = RegexConst.ENABLE_R18MOD)
	public void enableR18(@NotNull Bot bot,@NotNull WholeMessageEvent event){
		GroupMessageEvent.GroupSender sender = event.getSender();
		String msg = "权限不足";
		if("1592184684".equals(sender.getUserId())){
			R18 = "1";
			msg = "R18模式已启用";
		}
		bot.sendMsg(event, msg, false);
	}
	
	@MessageHandler(cmd = RegexConst.DISABLE_R18MOD)
	public void disableR18(@NotNull Bot bot,@NotNull WholeMessageEvent event){
		GroupMessageEvent.GroupSender sender = event.getSender();
		String msg = "权限不足";
		if("1592184684".equals(sender.getUserId())){
			R18 = "0";
			msg = "R18模式已关闭";
		}
		bot.sendMsg(event, msg, false);
	}
	
	@MessageHandler(cmd = RegexConst.REMIX_R18MOD)
	public void remixR18(@NotNull Bot bot,@NotNull WholeMessageEvent event){
		GroupMessageEvent.GroupSender sender = event.getSender();
		String msg = "权限不足";
		if("1592184684".equals(sender.getUserId())){
			R18 = "2";
			msg = "R18模式已混合";
		}
		bot.sendMsg(event, msg, false);
	}
	
	public Map<String,String> getParam(String[] param){
		Map<String,String> paramMap = Maps.newHashMap();
		Arrays.stream(param).forEach(o -> {
			if(o.contains(":")){
				String key = RegexUtils.regexGroup("(.*):(.*)", o,1);
				String value = RegexUtils.regexGroup("(.*):(.*)",o,2);
				if("num".equals(key)&&null!=value&& Strings.isNotEmpty(value)){
					int num = Integer.parseInt(value);
					value = num>9?"1":value;
				}
				paramMap.put(key,value);
			}
		});
		paramMap.put("r18", R18);
		paramMap.put("size","regular");
		paramMap.put("proxy","i.pixiv.re");
		return paramMap;	
	}
}
